/* Define Component Shortcuts */
let Ci = Components.interfaces;
let Cc = Components.classes;
let Cr = Components.results;

var backupPane = {
	_pane: null,

	init: function ()
	{
		this._pane = document.getElementById("paneRestore");
	},
	chooseFolder: function ()
	{
		var fp = Cc["@mozilla.org/filepicker;1"]
			.createInstance(Ci.nsIFilePicker);

		var bundleBackupPreferences = document.getElementById("bundleBackupPreferences");
		var title = bundleBackupPreferences.getString("chooseDestinationFolderTitle");
		fp.init(window, title, Ci.nsIFilePicker.modeGetFolder);
		fp.appendFilters(Ci.nsIFilePicker.filterAll);

		var currentDirPref = document.getElementById("browser.download.dir");

		fp.displayDirectory = Cc["@mozilla.org/file/directory_service;1"]
			.getService(Ci.nsIProperties)
			.get('Desk', Ci.nsIFile);

		if(currentDirPref && currentDirPref.exists())
		{
			fp.displayDirectory = currentDirPref;
		}

		var res = fp.show();
		if(res == Ci.nsIFilePicker.returnOK)
		{
			var currentDirPref = document.getElementById("backup.destination");
			if(currentDirPref)
			{
				currentDirPref.value = fp.file;
			}
		}
	},
	displayDestinationDirPref: function ()
	{
		// Used in defining the correct path to the folder icon.
		var ios = Components.classes["@mozilla.org/network/io-service;1"]
			.getService(Components.interfaces.nsIIOService);
		var fph = ios.getProtocolHandler("file")
			.QueryInterface(Components.interfaces.nsIFileProtocolHandler);
		var iconUrlSpec;

		var downloadFolder = document.getElementById('destinationFolder');
		var currentDirPref = document.getElementById('backup.destination');

		if(currentDirPref.value)
		{
			downloadFolder.label = currentDirPref.value.path;
			iconUrlSpec = fph.getURLSpecFromFile(currentDirPref.value);

			downloadFolder.image = "moz-icon://" + iconUrlSpec + "?size=16";
		}
    
		return undefined;
	}
};
